package com.situ.controller;

import com.situ.context.BaseContext;
import com.situ.domain.dto.ProductDTO;
import com.situ.domain.dto.SearchPageQueryDTO;
import com.situ.domain.entity.TProduct;
import com.situ.domain.entity.TProductCategory;
import com.situ.domain.vo.ProductQueryVO;
import com.situ.service.CategoryService;
import com.situ.service.ProductService;
import com.situ.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/product")
@RequiredArgsConstructor
@Tag(name = "商品接口")
public class ProuctController {

    private final ProductService productService;

    private final CategoryService categoryService;

    @GetMapping("/getProductList")
    @Operation(summary = "查询商品列表")
    public Result<ProductQueryVO> getProductList(@ParameterObject SearchPageQueryDTO pageQueryDTO) {
        log.info("查询商品列表接口:{}",pageQueryDTO);
        ProductQueryVO productQueryVO = productService.searchProductsByPage(pageQueryDTO);
        return Result.success(200,"查询成功",productQueryVO);
    }


    @PutMapping("/changeStatus")
    @Operation(summary = "修改商品状态")
    public Result changeStatus(Integer id,Integer status) {
        log.info("修改商品状态:{}", id);
        productService.lambdaUpdate()
                .eq(TProduct::getId, id)
                .set(TProduct::getStatus, status)
                .set(TProduct::getModifiedTime, new Date())
                .set(TProduct::getModifiedUser, BaseContext.getCurrentId().toString())
                .update();
        return Result.ok("修改商品状态成功");
    }


    @PostMapping("/addProduct")
    @Operation(summary = "添加商品")
    public Result addProduct(@RequestBody ProductDTO productDTO) {
        log.info("添加商品:{}", productDTO);
        productService.addProduct(productDTO);
        return Result.ok("添加商品成功");
    }

    @GetMapping("/getFirstCategory")
    @Operation(summary = "查询一级分类")
    public Result<List<TProductCategory>> getFirstCategory() {
        List<TProductCategory> list = categoryService.lambdaQuery()
                .eq(TProductCategory::getParentId, 0L)
                .list();
        return Result.success(200, "查询成功", list);
    }

    @GetMapping("/getSecondCategory")
    @Operation(summary = "查询二级分类")
    public Result<List<TProductCategory>> getSecondCategory(Integer id) {
        List<TProductCategory> list = categoryService.lambdaQuery()
                .eq(TProductCategory::getParentId, id)
                .list();
        return Result.success(200, "查询成功", list);
    }
}
